No data, just maps

par(pty="s")
mapBG1 = plotmap("Brandenburg Gate, Berlin", zoom = 15)

mapBG2 = plotmap("Brandenburg Gate, Berlin", zoom = 16, maptype="satellite")

Show traffic on bing maps

par(pty="s")

mapBG3 = GetBingMap(center="Brandenburg Gate, Berlin", zoom=12, extraURL="&mapLayer=TrafficFlow", 
                      apiKey=apiKey,verbose=1, destfile="BerlinTraffic.png")
## geocoding  Brandenburg Gate, Berlin 
## fetching  http://maps.google.com/maps/api/geocode/json?sensor=false&address=Brandenburg%20Gate,%20Berlin 
## [1] "http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/52.5162746,13.377704/12?mapSize=640,640&format=png&mapLayer=TrafficFlow&key=%20Ar6pkFx-XidvH9KD_2LEKf2fQpcssc3gMEiIxFQYQykF17MbqOZm31yA7Z5hxB27"
PlotOnStaticMap(mapBG3)

Customize google maps

no highways:

par(pty="s")

ManHatMap <- GetMap(center="Lower Manhattan", zoom=15, 
                      extraURL="&style=feature:road.highway|visibility:off")
  PlotOnStaticMap(ManHatMap)

Plots with spatial data

data(incidents)
col=as.numeric(incidents$Category)
par(pty="s")

mapSF_Z15 = plotmap(lat, lon, zoom = 15, col = col, pch=20, data = incidents)

#mapSF_Z13 = with(incidents, plotmap(lat, lon, zoom = 13, col = "Category", pch=20))

#lower zoom
mapSF_Z13 = RgoogleMaps::plotmap(lat, lon, zoom = 13, col = col, pch=20, data = incidents, alpha = 0.7)

Working offline

It would be wasteful to have to fetch a new map from the map server for each new plot! Instead, we pass the map object to the next calls:

par(pty="s")
SundayCrimes = subset(incidents, DayOfWeek=="Sunday")
col=as.numeric(SundayCrimes$violent)
plotmap(lat, lon, mapSF_Z13, col = col, pch=20, data = SundayCrimes, alpha = 0.5)

Crossing the -180/180 longitude boundary

par(mfrow=c(1,2),pty="s")
zoom <- 3
#Centering at -180:
lon=c(175,-180,-170, 160)
markers = cbind.data.frame(label= paste(1:4), lat=65,lon=lon, stringsAsFactors=FALSE)

terrmap <- GetMap(center=c(65, -180), zoom=zoom, maptype= "terrain", 
                  destfile="terrmap.png",markers=markers,verbose=1)
## [1] "http://maps.google.com/maps/api/staticmap?center=65,-180&zoom=3&size=640x640&maptype=terrain&format=png32&sensor=true&markers=label:1|65,175&markers=label:2|65,-180&markers=label:3|65,-170&markers=label:4|65,160"
lon=c(175-360,-180,-170, 160-360)

PlotOnStaticMap(terrmap,lat=65,lon=lon,col=1:(length(lon)),pch=20)

#Centering at 180:

lon=c(175,-180,-170, 160)
markers = cbind.data.frame(label= paste(1:4), lat=65,lon=lon, stringsAsFactors=FALSE)

terrmapb <- GetMap(center=c(65, 180), zoom=zoom, maptype= "terrain", 
                  destfile="terrmapb.png",markers=markers,verbose=1)
## [1] "http://maps.google.com/maps/api/staticmap?center=65,180&zoom=3&size=640x640&maptype=terrain&format=png32&sensor=true&markers=label:1|65,175&markers=label:2|65,-180&markers=label:3|65,-170&markers=label:4|65,160"
lon=c(175,-180+360,-170+360, 160)

PlotOnStaticMap(terrmapb,lat=65,lon=lon,col=1:(length(lon)),pch=20)

Conditional Plotting

Using the loa library and a larger crime dataset:

suppressWarnings(require(loa, quietly = TRUE))
suppressWarnings(require(latticeExtra, quietly = TRUE))

load("H:/DropboxHWR/Forschung/myPapers/JSS2015/FinalSubmission/RcodeForFigures/data/incidents2012.rda")
colnames(incidents)[10:11] = c("lon", "lat")

cols <- c("yellow", "darkred")
GoogleMap(~lat*lon|DayOfWeek,         #plot conditioned         
          data=incidents, map=NULL,
          groups=incidents$violent,                                               
          col=cols, cex=0.1, alpha=0.3,                 
          panel=panel.loaPlot2,                         
          key.groups=list(main="Violent", 
                          cex=1, col=cols))    

loa_mapSF <- getMapArg()

Heat Maps:

v <- ifelse(incidents$violent==TRUE, "Violent", "Non-violent")
#weekend versus weekday:
WE = ifelse(incidents$DayOfWeek %in% c("Saturday", "Sunday"),"weekend", "weekday")

useOuterStrips(GoogleMap(~lat*lon|WE+v,         #plot conditioned         
          data=incidents, map=loa_mapSF,
          panel=panel.kernelDensity,                          #surface plot control                                                                
          col.regions=c("lightyellow", "darkred"),            #
          alpha.regions=0.5,                                  #                       
          col=1,                                              #surface calculation    
          n=200, at=c(0.5,1,2.5,5,10,25,50), 
          scales=list(draw=FALSE), xlab="", ylab=""))     

#useOuterStrips(trellis.last.object())